package com.hbnu.associations;

import com.hbnu.associations.StudentClass;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class AssociationTest {

    public static void main(String[] args) {
        // 构建session
        // 加载配置
        Configuration configuration = new Configuration().configure();
        // 构建sessionFactory
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        // 获取session
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();

        Courses courses1 = new Courses();
        courses1.setCourseName("ORM");
        Courses courses2 = new Courses();
        courses2.setCourseName("java");

        Student student1 = new Student();
        student1.setName("jack");
        Student student2 = new Student();
        student2.setName("tom");

        courses1.getStudents().add(student1);
        courses1.getStudents().add(student2);

        session.save(courses1);
        session.save(student1);
        session.save(student2);

        transaction.commit();
        session.close();
        sessionFactory.close();


    }


    // public static void manyToOne(Session session){
    //     StudentClass studentClass = new StudentClass();
    //     studentClass.setClassName("2114班");
    //
    //     Student student1 = new Student();
    //     student1.setName("jack");
    //     Student student2 = new Student();
    //     student2.setName("tom");
    //
    //     studentClass.getStudents().add(student1);
    //     studentClass.getStudents().add(student2);
    //
    //     session.save(student1);
    //     session.save(student2);
    //     session.save(studentClass);
    // }
}
